package com.custom.action.condition;

import com.custom.action.condition.support.TableSupport;
import com.custom.action.core.TableSimpleSupport;
import com.custom.action.interfaces.ColumnParseHandler;
import com.custom.comm.utils.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 最终存储对象
 * @author Xiao-Bai
 * @since 2023/5/3 14:17
 */
public abstract class BaseCondition<T> implements Serializable {

    /**
     * 主表实体对象
     */
    private Class<T> entityClass;

    /**
     * 最终的sql条件语句
     */
    private StringBuilder finalConditional;

    /**
     * sql中的所有参数值
     */
    private List<Object> paramValues;

    /**
     * 表数据支持
     */
    private TableSupport tableSupport;

    /**
     * 函数式接口序列化解析对象
     */
    private ColumnParseHandler columnParseHandler;

    /**
     * 分页
     */
    protected Integer pageIndex;
    protected Integer pageSize;
    protected Boolean hasPageParams = false;

    protected StringBuilder customizeSql;

    /**
     * 后置SQL
     */
    protected StringBuilder suffixSql;

    /**
     * 拼接条件的前缀
     */
    private String currAppendSymbol = Constants.AND;


    public String getCustomizeSql() {
        if (this.customizeSql == null) {
            return Constants.EMPTY;
        }
        return customizeSql.toString();
    }

    protected void addCustomizeSql(String customizeSql) {
        if (this.customizeSql == null) {
            this.customizeSql = new StringBuilder();
        }
        this.customizeSql.append(Constants.WHITESPACE).append(customizeSql);
    }

    public String getSuffixSql() {
        if (this.suffixSql == null) {
            return Constants.EMPTY;
        }
        return suffixSql.toString();
    }

    protected void addSuffixSql(String suffixSql) {
        if (this.suffixSql == null) {
            this.suffixSql = new StringBuilder();
        }
        this.suffixSql.append(Constants.WHITESPACE).append(suffixSql);
    }

    protected void setPageParams(Integer pageIndex, Integer pageSize) {
        this.pageIndex = pageIndex;
        this.pageSize = pageSize;
        this.hasPageParams = true;
    }


    protected void dataStructureInit() {
        this.finalConditional = new StringBuilder();
        this.paramValues = new ArrayList<>();
    }

    public Class<T> getEntityClass() {
        return entityClass;
    }

    protected void setEntityClass(Class<T> entityClass) {
        AssertUtil.notNull(entityClass, "映射实体Class对象缺失");
        this.entityClass = entityClass;
    }

    public String getFinalConditional() {
        return this.finalConditional + getCustomizeSql();
    }

    /**
     * 添加条件
     * @param condition sql条件
     */
    protected void addCondition(String condition) {
        if (CustomUtil.isNotBlank(condition)) {
            this.finalConditional.append(condition);
        }
    }

    public List<Object> getParamValues() {
        return paramValues;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Boolean hasPageParams() {
        return hasPageParams;
    }

    public TableSupport getTableSupport() {
        return tableSupport;
    }

    protected void setTableSupport(TableSupport tableSupport) {
        this.tableSupport = tableSupport;
    }

    public ColumnParseHandler getColumnParseHandler() {
        return columnParseHandler;
    }

    protected void setColumnParseHandler(ColumnParseHandler columnParseHandler) {
        this.columnParseHandler = columnParseHandler;
    }

    protected void addParams(Object param) {
        if (param instanceof Collection) {
            this.paramValues.addAll((Collection<?>) param);
            return;
        }
        this.paramValues.add(param);
    }

    protected void addParams(Object... params) {
        paramValues.addAll(Arrays.asList(params));
    }

    protected void addParams(List<Object> params) {
        this.paramValues.addAll(params);
    }

    protected void wrapperInitialize(Class<T> entityClass, TableSupport tableSupport) {
        setEntityClass(entityClass);
        if (tableSupport == null) {
            this.tableSupport = new TableSimpleSupport<>(getEntityClass());
        }else {
            this.tableSupport = tableSupport;
        }
        this.columnParseHandler = new DefaultColumnParseHandler(entityClass, this.tableSupport);
        this.dataStructureInit();
    }

    protected String getCurrAppendSymbol() {
        return currAppendSymbol;
    }

    protected void setCurrAppendSymbol(String currAppendSymbol) {
        this.currAppendSymbol = currAppendSymbol;
    }

    protected void wrapperInitialize(Class<T> entityClass) {
        wrapperInitialize(entityClass, null);
    }

}
